<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="js/jQuery.js" type="text/javascript"></script>
<script>
    var oldname='';
	function setValue(name,QQ,email){
		document.getElementById("username").value=name;
		document.getElementById("QQ").value=QQ;
		document.getElementById("email").value=email;
		oldname=name;
	}
	function ajax(){
		       $(".warning").html("");
               var username=$("input[name='username']").val();
               if(oldname != username){
	                $.ajax({
			                type: "POST",
			                url: "usernameAjax.php",
			                data: "username="+username,
			                success: function(msg){
			                    if(msg==1){
			                            $(".warning").html("用户名已经存在");
			                    }else
			                            $(".warning").html("可以使用");
			                }
	                });
	           }
    }
	
</script>

<?php include( "config/ini.php"); ?>

<form action="" method="POST">
<table width="500">
	<?php
	    if(isset($_GET["upId"])){
	    	$upId=$_GET["upId"];
	    	echo "<tr>";
			echo "   <td width='150'>id:</td>";
			echo "   <td><input type='text' name='id' value='{$upId}' readonly/></td>";
			echo "</tr>";
	    }
    ?>
	<tr>
		<td width="150">username:</td>
		<td><input type="text" name="username" onchange="ajax();"/>
			<span class="warning"></span>
		</td>
	</tr>
	<tr>
		<td width="150">QQ:</td>
		<td><input type="text" name="QQ" /></td>
	</tr>
	<tr>
		<td width="150">email:</td>
		<td><input type="text" name="email" /></td>
	</tr>
    <tr>
    	<td></td>
    	<td align="right"><input type="submit" name="submited" value="提交"/></td>
    </tr>
</table>
</form>
<?php
    if(isset($_GET['upId'])){
		$upId=$_GET['upId'];
		$sql=mysql_query("select * from phpguestbook where id={$upId}");
		$resRow=  mysql_fetch_array($sql);
		echo "<script>setValue('{$resRow['username']}','{$resRow['QQ']}','{$resRow['email']}');</script>";
	}
?>

<?php
    if(isset($_POST['submited'])){//提交按钮
    	$username=$_POST['username'];
	    $QQ=$_POST['QQ'];
		$email=$_POST['email'];
		
		if($_POST['id']){//修改
			$upId=$_POST['id'];
			$upsql="UPDATE  `student`.`phpguestbook` SET  `username` =  '$username',`QQ` =  '$QQ',`email` =  '$email' WHERE  `phpguestbook`.`id` ={$upId}";
            $updataquery=mysql_query($upsql);
			if($updataquery){
	    	    header("location:sqlStudy.php");
	        }else{
	    	    echo "alert('修改失败！')";
	        }
		}else{//新增
		    $insertSql=mysql_query("INSERT INTO `student`.`phpguestbook` (`id`, `username`, `QQ`, `email`) VALUES (NULL, '$username', '$QQ', '$email')");
	        if($insertSql){
	    	    header("location:sqlStudy.php");
	        }else{
	    	    echo "alert('新增失败！')";
	        }
		}
    }
?>

